package logic

import (
	"context"
	"errors"
	"house_srv/models/mysql"

	"house_srv/house_srv"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateHouseLogic {
	return &CreateHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateHouseLogic) CreateHouse(in *house_srv.CreateHouseRequest) (*house_srv.CreateHouseResponse, error) {
	// todo: add your logic here and delete this line

	house := mysql.House{
		Name:    in.Name,
		Price:   float64(in.Price),
		Address: in.Address,
		Hav:     in.Hav,
		Img:     in.Img,
	}

	err := house.Create()

	if err != nil {
		return nil, errors.New("房源发布失败")
	}

	return &house_srv.CreateHouseResponse{Id: int64(house.ID)}, nil
}
